//
//  HEGoodsDetailsCommentsCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/22.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodsDetailsCommentsCell.h"
#import "HEPhotosContainerView.h"
#import "HDRatingStarView.h"
#import "HEContaintView.h"

@interface HEGoodsDetailsCommentsCell ()

//@property (weak, nonatomic) IBOutlet UIImageView *userHeadImg;
//@property (weak, nonatomic) IBOutlet UILabel *nameLb;
//@property (weak, nonatomic) IBOutlet UILabel *timeLb;

@property (strong, nonatomic) UIImageView *userHeadImg;
@property (strong, nonatomic) UILabel *nameLb;
@property (strong, nonatomic) UILabel *timeLb;

@property (nonatomic, strong) HDRatingStarView *startView;
@property (nonatomic, strong) UILabel *cotentLb;
@property (nonatomic, strong) HEPhotosContainerView *images;
//@property (nonatomic, strong) HEContaintView *images;

@property (nonatomic, strong) MASConstraint *he;

@property (nonatomic, assign) BOOL hasCreate;

@end

@implementation HEGoodsDetailsCommentsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // Fix the bug in iOS7 - initial constraints warning
        self.contentView.bounds = [UIScreen mainScreen].bounds;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundColor = KWhiteColor;
        self.qmui_borderPosition = QMUIViewBorderPositionBottom;
        self.qmui_borderColor = KHexColor(@"F5F5F5");
        
        [self initUI];
        [self setpData];
    }
    return self;
}

- (void)initUI {
    
    _userHeadImg = [[UIImageView alloc] init];
    _userHeadImg.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_userHeadImg];
    
    _nameLb = [[UILabel alloc] init];
    _nameLb.font = DYFont(16);
    [self.contentView addSubview:_nameLb];
    
    _timeLb = [[UILabel alloc] init];
    _timeLb.font = DYFont(14);
    _timeLb.textColor = KHexColor(@"757575");
    [self.contentView addSubview:_timeLb];
    
    [_userHeadImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.width.height.mas_equalTo(50);
        make.left.equalTo(self.contentView).mas_offset(15);
    }];
    
    [_timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userHeadImg);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-15);
    }];
    [_timeLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_timeLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}

- (void)setpData {
     
    
    _userHeadImg.layer.masksToBounds = YES;
    _userHeadImg.layer.cornerRadius = 25;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
 
    // 星星评分
    _startView = [[HDRatingStarView alloc] init];
    _startView.renderColor = KHexColor(@"F67605");
    _startView.allowTouchToSelectScore = NO;
    _startView.score = 0;
    _startView.starWidth = 15;
    _startView.starImage = IMAGE_NAMED(@"rating_sel");
    [self.contentView addSubview:_startView];
   
    
    // 评论
    _cotentLb = [UILabel new];
    _cotentLb.font = DYFont(15);
    _cotentLb.numberOfLines = 0;
//    _cotentLb.backgroundColor = UIColor.orangeColor;
    [self.contentView addSubview:_cotentLb];
    
    // 可去掉
    _cotentLb.preferredMaxLayoutWidth = KScreenWidth-20 - 50;
    [_cotentLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
   // 名称
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(_userHeadImg.mas_right).mas_offset(10);
        make.right.equalTo(_timeLb.mas_left).mas_offset(-10);
        make.height.mas_offset(20);
    }];
   
   [_startView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.top.equalTo(_nameLb.mas_bottom).mas_offset(5);
      make.left.equalTo(_nameLb.mas_left);
      make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
      make.height.mas_offset(15);
   }];


    [_cotentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_startView.mas_bottom).mas_offset(10);
        make.left.equalTo(_nameLb.mas_left);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
    }];
    
    
//    HEContaintView *images = [[HEContaintView alloc] init];
//    [self.contentView addSubview:images];
//     _images = images;
//    [images mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_cotentLb.mas_bottom).mas_offset(10);
//        make.left.equalTo(_nameLb.mas_left);
//        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
//        make.height.mas_equalTo(220);
//        make.bottom.equalTo(self.contentView).mas_offset(-10);
//    }];
//
//    #warning 动态高度暂时未解决。。。
//    images.photoViewChangeHeightBlock = ^(CGFloat photoViewHeight) {
//
//        [self.images mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(photoViewHeight);
//        }];
//
//        [self refreshLayout];
//
//    };
    
 
    HEPhotosContainerView *images = [[HEPhotosContainerView alloc] initWithMaxItemsCount:6];
//    images.backgroundColor = UIColor.orangeColor;
    [self.contentView addSubview:images];
    [images mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cotentLb.mas_bottom).mas_offset(10);
        make.left.equalTo(_nameLb.mas_left);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
    }];
    _images = images;
}

- (void)layoutSubviews {
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath{
    
    HEGoodsCommentModel *model = formModel.dg_commnets[indexPath.row];
    [_userHeadImg sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:IMAGE_NAMED(@"noImage")];
    
    _startView.score = model.stars;
    _nameLb.text = model.memberName;
    _cotentLb.text = model.consultContent;
    _timeLb.text = model.kconsultAddtime;
    
    _images.photoNamesArray = model.kemail;
    if (model.kemail.count == 0) {
        self.images.hidden = YES;
        [_cotentLb mas_updateConstraints:^(MASConstraintMaker *make) {
           _he = make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
        }];
    }else {
        self.images.hidden = NO;
        [_cotentLb mas_updateConstraints:^(MASConstraintMaker *make) {
            [_he uninstall];
        }];
    }
}

- (void)refreshLayout {
    //返回cell之前重新刷新约束，重新计算高度
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
